package de.metalcon.gwt.server.modules;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import de.metalcon.gwt.server.hibernate.User;
import de.metalcon.gwt.shared.ContentTransmitter;

public class MySQLModuleServer extends AModuleServer {

	public MySQLModuleServer(HashMap<String, Object> requestVars, ServletContext context) {
		super(requestVars, context);
	}

	@Override
	public ContentTransmitter getTransmitter() {
		ContentTransmitter t = new ContentTransmitter();

		SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
		Session session = sessionFactory.openSession();
		List users = session.createQuery("from User where ID<3").list();

		int i = 0;
		for (Object o : users) {
			User u = (User) o;

			ContentTransmitter c = new ContentTransmitter();

			/*for (Method m : u.getClass().getMethods()) {
				try {
					/*Object value = m.invoke(u, (Object[]) null);
					if(value!=null && (value instanceof String || value instanceof Integer)){
						c.setString(m.getName(), value.toString());
					}
					c.setString(m.getName(), ""+i++);
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}*/
			c.setString("asdf", ""+i++);
			t.addListElement("users", c);
		}
		session.close();

		for (Map.Entry<String, Object> entry : requestVars.entrySet()) {
			t.setString(entry.getKey(), (String) entry.getValue());
		}
		return t;
	}
}
